%include "I:\workdata\706727\Build\Directories_And_SampleRestrictions.sas";
libname save "I:\Workdata\706727\Build\Data";
libname exp "I:\Workdata\706727\Build\Data";

* Hospital Care  *;
data pop(drop=LPRadmSourceYear);
set raw.LPRadmv (
where=(LPRadmSourceYear>1994 and c_spec ne "50" and c_spec ne "52"));
year=year(d_inddto);
month=month(d_inddto);
run;

data suipop;
set pop (where=(c_kontaars="4"));
spec2=c_spec*1;
run;

data suipers;
set suipop(keep=pnr);
run;

proc sort data=suipers nodup; by pnr; run;

proc sql;
create table suiperspop as select * from suipers as a

left join pop as b on a.pnr=b.pnr;
quit;

data suiperspop;
set suiperspop;
spec2=c_spec*1;
run;

data pricehel;
set raw.Lprdrgshelv;
run;

data priceamb;
set raw.Lprdrgsambv;
run;

data priceamb;
set priceamb;
noprice=(c_casemix="MG90G");
run;

proc sql;
create table fpriceamb as select distinct pnr,recnum,d_inddto,c_adiag,c_kontaars,c_pattype,c_spec,max(noprice) as noprice,sum(v_pris) as v_pris from priceamb
group by pnr,recnum
order by pnr,recnum;
quit;

data pricehel;
set pricehel;
if c_kontaars="." then c_kontaars="";
spec2=c_spec*1;
run;

/* Merge hel */
proc sql;
create table mergeatt as select * from suiperspop(keep=pnr d_inddto c_adiag c_kontaars c_pattype spec2 c_udm) as a

left join pricehel(keep=pnr v_totpris c_adiag c_kontaars c_pattype spec2 c_udm) as b on a.pnr=b.pnr and a.c_adiag=b.c_adiag and a.c_kontaars=b.c_kontaars and a.c_pattype=b.c_pattype and a.spec2=b.spec2 and a.c_udm=b.c_udm;
quit;

data mergeatt(where=(year>=2002));
set mergeatt;
year=year(d_inddto);
run;

data nomisspricehel;
set pricehel (where=(v_totpris~=. ));
run;

data match;
set mergeatt(where=(c_pattype="0"));
year=year(d_inddto);
match=(v_totpris~=.);
run;

proc freq data=match;
table year*match;
run;

data tjek;
set pricehel(keep=pnr v_totpris c_adiag c_kontaars c_pattype c_spec c_udm Lprdrgshelsourceyear);
spec2=c_spec*1;
run;

proc freq data=tjek;
table Lprdrgshelsourceyear*spec2;
run;

proc freq data=suiperspop;
table year*c_udm;
run;

/* Merge amb */
data fpriceamb;
set fpriceamb;
if c_kontaars="." then c_kontaars="";
spec2=c_spec*1;
ambdat=1;
run;

proc sql;
create table mergeamb as select * from suiperspop(keep=pnr year d_inddto c_adiag c_kontaars c_pattype spec2) as a

left join fpriceamb(keep=pnr ambdat v_pris c_adiag c_kontaars c_pattype spec2 d_inddto where=(c_pattype="2")) as b on a.pnr=b.pnr and a.c_adiag=b.c_adiag and a.c_kontaars=b.c_kontaars and a.c_pattype=b.c_pattype and a.spec2=b.spec2 and a.d_inddto=b.d_inddto
left join fpriceamb(keep=pnr ambdat v_pris noprice c_adiag c_pattype spec2 d_inddto where=(c_pattype="3") rename=(v_pris=amb_pris)) as c on a.pnr=c.pnr and a.c_adiag=c.c_adiag and a.c_pattype=c.c_pattype and a.spec2=c.spec2 and a.d_inddto=c.d_inddto;
quit;

data match;
set mergeamb(where=(c_pattype="3" or c_pattype="2"));
year=year(d_inddto);
ermatch=(amb_pris~=.);
ambmatch=(v_pris~=.);
run;

proc freq data=match(where=(c_pattype="3"));
table year*ermatch;
run;

proc freq data=match(where=(c_pattype="2"));
table year*ambmatch;
run;

/* Append data sets */
data merged(where=(year>=2002 and year<=2013));
set mergeatt(where=(c_pattype="0")) mergeamb(where=(c_pattype~="0"));
run;

/* sui prices */
data suiprice;
set merged(where=(c_kontaars="4"));
match=((c_pattype="0" and v_totpris~=. and v_totpris~=0) or (c_pattype="2" and v_pris~=. and v_pris~=0) or (c_pattype="3" and amb_pris~=. and amb_pris~=0));
run;

proc freq data=suiprice(where=(match=0 and c_pattype="3"));
table year*noprice;
run;

proc freq data=merged;
table c_kontaars*c_pattype;
run;


/* Check */
data nopricepers(keep=pnr);
set suiprice(where=(match=0 and c_pattype="3" and amb_pris=0));
run;

proc sort data=nopricepers nodup;
by pnr;
run;

proc sql;
create table nopriceamb as select * from nopricepers as a

left join merged as b on a.pnr=b.pnr;
quit;

proc sort data=nopriceamb;
by pnr d_inddto c_pattype;
run;

data prev;
set nopriceamb(where=((c_pattype="0" and v_totpris~=.) or (c_pattype="3" and c_kontaars="4" and noprice=1))) ;
lagd_inddto=lag(d_inddto);
lagpnr=lag(pnr);
lagc_adiag=lag(c_adiag);
lagc_kontaars=lag(c_kontaars);
lagv_totpris=lag(v_totpris);
* Exactly the same date *;
if noprice=1 and c_kontaars="4" and lagc_kontaars~="4" and d_inddto=lagd_inddto and lagpnr=pnr then amb_pris=lagv_totpris;
if noprice=1 and c_kontaars="4" and lagc_kontaars="4" and d_inddto=lagd_inddto and lagpnr=pnr then truezero=1;
if noprice=1 and c_kontaars="4" then prevind=(d_inddto=lagd_inddto and lagpnr=pnr );
* Within five days of *;
if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 and (amb_pris=. or amb_pris=0) and d_inddto-lagd_inddto<5 and lagc_kontaars~="4"  and lagpnr=pnr then amb_pris=lagv_totpris;
if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 and (amb_pris=. or amb_pris=0) and d_inddto-lagd_inddto<5 and lagc_kontaars="4"  and lagpnr=pnr and truezero~=1 then truezero=1;
if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 then prevind=(d_inddto-lagd_inddto<5 and lagpnr=pnr );
run;

proc freq data=prev(where=(noprice=1 and c_kontaars="4"));
table year*prevind;
run;

proc sort data=prev;
by pnr descending d_inddto c_pattype;
run;

data prev;
set prev;
lagd_inddto=lag(d_inddto);
lagpnr=lag(pnr);
lagc_adiag=lag(c_adiag);
lagc_kontaars=lag(c_kontaars);
lagv_totpris=lag(v_totpris);

if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 and lagd_inddto-d_inddto<5 and lagpnr=pnr and lagc_kontaars~="4" then amb_pris=lagv_totpris;
if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 and lagd_inddto-d_inddto<5 and lagpnr=pnr and lagc_kontaars="4" and truezero~=1 then truezero=1;
if noprice=1 and c_kontaars="4" and (lagc_adiag=c_adiag or (substr(lagc_adiag,1,2)="DT" or substr(lagc_adiag,1,2)="DS" or substr(lagc_adiag,1,2)="DX" or substr(lagc_adiag,1,2)="DF"))
and prevind=0 then prevind=(lagd_inddto-d_inddto<5 and lagpnr=pnr );

if noprice=1 then stillnoprice=(truezero~=1 and (amb_pris=0 or amb_pris=.));
run;

proc freq data=prev(where=(noprice=1 and c_kontaars="4"));
table year*stillnoprice;
run;

proc sort data=prev;
by pnr d_inddto c_pattype;
run;

/* Gather data again */
data missambprices;
set prev(where=(noprice=1 and c_kontaars="4" and c_pattype="3"));
rename amb_pris=missamb_pris;
run;

data suipricemerge;
set suiprice(where=(c_pattype~="3" or (c_pattype="3" and match=1))) missambprices;
run;

data suipricemerge;
set suipricemerge;
matchf=((c_pattype="0" and v_totpris~=. and v_totpris~=0) or (c_pattype="2" and v_pris~=. and v_pris~=0)
or (c_pattype="3" and amb_pris~=. and amb_pris~=0) or (c_pattype="3" and missamb_pris~=. and missamb_pris~=0) or (c_pattype="3" and missamb_pris=0 and truezero=1));
price=coalesce(v_totpris,v_pris,amb_pris,missamb_pris);
run;

proc freq data=suipricemerge;
table year*matchf;
run;

/* Merge back on normal sui data */
proc sql;
create table suipop2(where=(year>=2002 and year<=2013)) as select * from suipop(keep=pnr c_kontaars recnum year d_inddto c_pattype spec2 c_adiag) as a 

left join suipricemerge(keep=pnr year d_inddto c_pattype spec2 c_adiag price) as b 
on a.pnr=b.pnr and a.c_adiag=b.c_adiag and a.c_pattype=b.c_pattype and a.spec2=b.spec2 and a.d_inddto=b.d_inddto;
quit;

/** Use average diagnosis-specific prices **/
data suipop3;
set suipop2(where=(c_kontaars="4" ));
diag2=substr(c_adiag,2,2);
diag1=substr(c_adiag,2,1);
month=month(d_inddto);
if month<4 then year2=year-1;
if month>=4 then year2=year;
if year2=2000 then dprice=price*1000*(145.46/97.11); 
if year2=2001 then dprice=price*1000*(145.46/100.35); 
if year2=2002 then dprice=price*1000*(145.46/102.59); 
if year2=2003 then dprice=price*1000*(145.46/105.57); 
if year2=2004 then dprice=price*1000*(145.46/106.81); 
if year2=2005 then dprice=price*1000*(145.46/108.05);
if year2=2006 then dprice=price*1000*(145.46/113.25); 
if year2=2007 then dprice=price*1000*(145.46/116.90); 
if year2=2008 then dprice=price*1000*(145.46/123.02);
if year2=2009 then dprice=price*1000*(145.46/126.86);
if year2=2010 then dprice=price*1000*(145.46/128.76);
if year2=2011 then dprice=price*1000*(145.46/129.52); 
if year2=2012 then dprice=price*1000*(145.46/131.93); 
if year2=2013 then dprice=price*1000*(145.46/132.44);
if year2=2014 then dprice=price*1000*(145.46/133.84);
if year2=2015 then dprice=price*1000*(145.46/134.92);
if year2=2016 then dprice=price*1000*(145.46/136.66); /* data only exist until 2016 - does not matter as average price is used */
run;

proc sort data=suipop3;
by c_pattype diag1;
run;

proc means data=suipop3 nway noprint;
var dprice;
by  c_pattype diag1;
output out=avprice1 mean=mprice1;
run;

data exp.suiavprice1;
set avprice1;
run;

proc sort data=suipop3;
by c_pattype diag2;
run;

proc means data=suipop3 nway noprint;
var dprice;
by  c_pattype diag2;
output out=avprice2 mean=mprice2;
run;

data exp.suiavprice2;
set avprice2;
run;

proc sort data=suipop3;
by c_pattype ;
run;

proc means data=suipop3 nway noprint;
var dprice;
by  c_pattype ;
output out=avprice3 mean=mprice3;
run;

data exp.suiavprice3;
set avprice3;
run;

